<?php

namespace fast;

use think\Db;

use think\Exception;
use fast\Http;
/**
 * Http 请求类
 */
class Tongcheng
{

    public static $url = "http://183.62.170.46:38092/tcpapi/orderdist";
    public static $key = "0b15b75c-94b4-482b-a462-fd21e45a50f5";
    public static $customerCode = "1663339";



    // public static $url = "https://api.kdniao.com/tcpapi/orderdist";
    // public static $key = "c3163f67-53d0-4637-84ee-03c5fd077e72";
    // public static $customerCode = "1885104";



    //门店查询
    public static function createMendian($id)
    {
        
        $post_params['customerCode'] = self::$customerCode;
        $post_params['interfaceType'] = '2211';
        $post_params['dataSign'] = '';
        $post_params['requestData'] = '';

        $mendian = Db::table('fa_pifa_shangjia')->find($id);
    
        $data['storeCode'] = 'XBXD'.$mendian['id']; //平台门店编码
        $data['storeName'] = $mendian['name']; //门店名称
        $data['storeGoodsType'] = '4'; //门店名称
        $data['provinceName'] = $mendian['province']; //门店名称
        $data['cityName'] = $mendian['city']; //门店名称
        $data['areaName'] = $mendian['area']; //门店名称
        $data['address'] = $mendian['address']; //门店名称
        $data['lbsType'] = '3'; //坐标类型1：百度地图 2：高德地图 3：腾讯地图
        $data['longitude'] = $mendian['lng']; //门店经度
        $data['latitude'] = $mendian['lat']; //门店纬度
        $data['contactName'] = $mendian['consignee']; //门店联系人姓名
        $data['mobile'] = $mendian['phone']; //门店联系人手机号
        $data['phone'] = $mendian['phone']; //门店联系人座机号
        $data['startBusinessTime'] = '08:00'; //营业开始时间如
        $data['endBusinessTime'] = '22:00'; //营业开始时间如
        $data['attachType'] = 1; //附件类型(有上传附件时，该字段必填)1：URL地址2：Base64（不支持）3：图片hash值
        $data['idCardFront'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/sfz_zheng.jpg'; //身份证正面图片(使用美团、蜂鸟必填)
        $data['idCardBack'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/sfz_fan.jpg'; //身份证正面图片(使用美团、蜂鸟必填)
        $data['idCard'] = '652801199309300014'; //身份证号(使用美团、蜂鸟必填)
        $data['idCardName'] = '王智威'; //身份证姓名(使用美团、蜂鸟必填)
        $data['license'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/zhizhao.jpg'; //营业执照图片(使用美团、蜂鸟必填)
        $data['creditCode'] = '91652801MAEBN59Q1L'; //社会统一信用代码(使用美团、蜂鸟必填)
        $data['shopPicture'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/md.jpg'; //门店照图片(使用美团、蜂鸟必填)
        $data['handheldLicencePic'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/shouchi.jpg'; //门店拥有人手持身份证、营业执照图片

       
        $post_params['requestData'] = json_encode($data,JSON_UNESCAPED_UNICODE);
        $post_params['dataSign'] = json_encode($data,JSON_UNESCAPED_UNICODE);


        $md5Str = md5($post_params['requestData'] . self::$key); // true 返回二进制格式

        // 2. Base64 编码
        $base64Str = base64_encode($md5Str);

        // 3. URL 编码
        $datasign = urlencode($base64Str);

        $post_params['dataSign'] = $datasign; 
        $headers = ['Content-type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest(self::$url, $post_params, 'POST');

       
        $res = json_decode($response['msg'],true);
       
        if (isset($res['resultCode']) &&  $res['resultCode'] && $res['resultCode'] == 100) {
            $updata['kdnStoreCode'] = $res['data']['kdnStoreCode'];
            $updata['kdnStoreStatus'] = $res['data']['kdnStoreStatus'];
            $map3['id'] = $id;
            Db::table('fa_pifa_shangjia')->where($map3)->update($updata);
        }
    }



    //门店查询
    public static function editMendian($id)
    {
        
        $post_params['customerCode'] = self::$customerCode;
        $post_params['interfaceType'] = '2212';
        $post_params['dataSign'] = '';
        $post_params['requestData'] = '';

        $mendian = Db::table('fa_pifa_shangjia')->find($id);
    
        $data['storeCode'] = 'XBXD'.$mendian['id']; //平台门店编码
        $data['storeName'] = $mendian['name']; //门店名称
        $data['storeGoodsType'] = '4'; //门店名称
        $data['provinceName'] = $mendian['province']; //门店名称
        $data['cityName'] = $mendian['city']; //门店名称
        $data['areaName'] = $mendian['area']; //门店名称
        $data['address'] = $mendian['address']; //门店名称
        $data['lbsType'] = '3'; //坐标类型1：百度地图 2：高德地图 3：腾讯地图
        $data['longitude'] = $mendian['lng']; //门店经度
        $data['latitude'] = $mendian['lat']; //门店纬度
        $data['contactName'] = $mendian['consignee']; //门店联系人姓名
        $data['mobile'] = $mendian['phone']; //门店联系人手机号
        $data['phone'] = $mendian['phone']; //门店联系人座机号
        $data['startBusinessTime'] = '08:00'; //营业开始时间如
        $data['endBusinessTime'] = '22:00'; //营业开始时间如
   

        $data['attachType'] = 1; //附件类型(有上传附件时，该字段必填)1：URL地址2：Base64（不支持）3：图片hash值
        $data['idCardFront'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/sfz_zheng.jpg'; //身份证正面图片(使用美团、蜂鸟必填)
        $data['idCardBack'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/sfz_fan.jpg'; //身份证正面图片(使用美团、蜂鸟必填)
        $data['idCard'] = '652801199309300014'; //身份证号(使用美团、蜂鸟必填)
        $data['idCardName'] = '王智威'; //身份证姓名(使用美团、蜂鸟必填)
        $data['license'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/zhizhao.jpg'; //营业执照图片(使用美团、蜂鸟必填)
        $data['creditCode'] = '91652801MAEBN59Q1L'; //社会统一信用代码(使用美团、蜂鸟必填)
        $data['shopPicture'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/md.jpg'; //门店照图片(使用美团、蜂鸟必填)
        $data['handheldLicencePic'] = 'http://xiaoxiongimg.xinanqy.com/ziliao/shouchi.jpg'; //门店拥有人手持身份证、营业执照图片


  
        $post_params['requestData'] = json_encode($data,JSON_UNESCAPED_UNICODE);
        $post_params['dataSign'] = json_encode($data,JSON_UNESCAPED_UNICODE);


        $md5Str = md5($post_params['requestData'] . self::$key); // true 返回二进制格式

        // 2. Base64 编码
        $base64Str = base64_encode($md5Str);

        // 3. URL 编码
        $datasign = urlencode($base64Str);

        $post_params['dataSign'] = $datasign; 
        $headers = ['Content-type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest(self::$url, $post_params, 'POST');

       
        $res = json_decode($response['msg'],true);
       
        // if ($res['resultCode'] && $res['resultCode'] == 100) {
        //     $updata['kdnStoreCode'] = $res['data']['kdnStoreCode'];
        //     $updata['kdnStoreStatus'] = $res['data']['kdnStoreStatus'];
        //     $map3['id'] = $id;
        //     Db::table('fa_pifa_shangjia')->where($map3)->update($updata);
        // }
     

    }


    
     //门店查询
    public static function chaxunmendian($id)
    {
        


        $mendian = Db::table('fa_pifa_shangjia')->find($id);

        $data['kdnStoreCode'] = $mendian['kdnStoreCode'];
        $data['storeCode'] = 'XBXD'.$mendian['id'];

        $post_params['customerCode'] = self::$customerCode;
        $post_params['interfaceType'] = '2218';
        $post_params['dataSign'] = '';
        $post_params['requestData'] = '';
        $post_params['requestData'] = json_encode($data,JSON_UNESCAPED_UNICODE);
        $post_params['dataSign'] = json_encode($data,JSON_UNESCAPED_UNICODE);


        $md5Str = md5($post_params['requestData'] . self::$key); // true 返回二进制格式

        // 2. Base64 编码
        $base64Str = base64_encode($md5Str);

        // 3. URL 编码
        $datasign = urlencode($base64Str);

        $post_params['dataSign'] = $datasign; 
        $headers = ['Content-type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest(self::$url, $post_params, 'POST');

       
        $res = json_decode($response['msg'],true);

        var_dump($res['data']['storeDetail']);
        if ($res['resultCode'] && $res['resultCode'] == 100) {
            


        }
     

    }

    //创建订单
    public static function createOrder($id,$data)
    {
            


        $Order = new \app\admin\model\pifa\Order;
        $info = $Order->with(['mendian'])->find($id);

        $info = $info->toArray();

        $map['order_id'] = $id;
        $OrderitemModel = new \app\admin\model\pifa\Orderitem;

        $list = $OrderitemModel::with(['goods'])->where($map)->select();
     

        $info['item'] =  $list;
        $citylist = getcitylist();


        $data['storeBusinessType'] = 1; //门店业务类型
        $data['storeCode'] = 'XBXD'.$info['mendian']['id']; //平台门店编码
        $data['productTypeCode'] = 'B';
        $data['intraCode'] = 'SFTC,MTPS';
        $data['appointmentType'] = 0;
        $data['travelWay'] = 0;
        $data['deliveryType'] = '1';
        $data['lbsType'] = '3';
        $data['goodsType'] = '13';
        $data['goodsNum'] = '1';
        $data['totalWeight'] = '1';
        $data['totalVolume'] = '1';
        $data['insured'] = 0;
        $data['orderName'] =  $info['mendian']['consignee'];
        $data['orderMobile'] =  $info['mendian']['phone'];
        $data['goodsList'] =  [];


  
        foreach ($list as $key => $value) {
            $data['goodsList'][$key]['categoryThree'] = '2458';
            $data['goodsList'][$key]['goodsName'] = $value['goods_name'];
            $data['goodsList'][$key]['unit'] = '件';
            $data['goodsList'][$key]['quantity'] = $value['goods_num'];;
            $data['goodsList'][$key]['goodsPrice'] = $value['price'];
        }

        


        $data['sender']['provinceName'] = $info['mendian']['province'];
        $data['sender']['cityName'] = $info['mendian']['city'];
        $data['sender']['areaName'] = $info['mendian']['area'];


        $data['sender']['address'] = $info['mendian']['address'];
        $data['sender']['name'] = $info['mendian']['name'];
        $data['sender']['mobile'] = $info['mendian']['phone'];
        $data['sender']['longitude'] = $info['mendian']['lng'];
        $data['sender']['latitude'] = $info['mendian']['lat'];




        $data['receiver']['provinceName'] = $citylist[$info['province_id']]['name'];
        $data['receiver']['cityName'] = $citylist[$info['city_id']]['name'];
        $data['receiver']['areaName'] = $citylist[$info['area_id']]['name'];
        $data['receiver']['address'] = $info['address'];
        $data['receiver']['name'] = $info['consignee'];
        $data['receiver']['mobile'] = $info['phone'];
        $data['receiver']['longitude'] = $info['lng'];
        $data['receiver']['latitude'] = $info['lat'];



        var_dump($data);

        $post_params['customerCode'] = self::$customerCode;
        $post_params['interfaceType'] = '2201';
        $post_params['dataSign'] = '';
        $post_params['requestData'] = '';
        $post_params['requestData'] = json_encode($data,JSON_UNESCAPED_UNICODE);
        $post_params['dataSign'] = json_encode($data,JSON_UNESCAPED_UNICODE);

       

  

      


        $md5Str = md5($post_params['requestData'] . self::$key); // true 返回二进制格式

        // 2. Base64 编码
        $base64Str = base64_encode($md5Str);

        // 3. URL 编码
        $datasign = urlencode($base64Str);

        $post_params['dataSign'] = $datasign; 
        $headers = ['Content-type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest(self::$url, $post_params, 'POST');

       
        $res = json_decode($response['msg'],true);

        var_dump($res);

        

    }

}
